package com.fr.web.face;

import com.fr.base.CodeUtils;
import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.privilege.authentication.Authentication;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.web.core.WebUtils;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class WebURLDef extends AbstractNodeDef
{
  private String url = "";
  private Parameter[] parameters = null;

  public WebURLDef()
  {
  }

  public WebURLDef(String paramString)
  {
    setName(paramString);
  }

  public String getURL()
  {
    return this.url;
  }

  public void setURL(String paramString)
  {
    this.url = paramString;
  }

  public Parameter[] getParameters()
  {
    return this.parameters;
  }

  public void setParameters(Parameter[] paramArrayOfParameter)
  {
    this.parameters = paramArrayOfParameter;
  }

  public Access createURI(HttpServletRequest paramHttpServletRequest, Authentication paramAuthentication)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    String str = getURL();
    try
    {
      str = TemplateUtils.render(str, WebUtils.createTemplateMap(paramHttpServletRequest));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    int i = (str.indexOf("?") != -1) ? 1 : 0;
    localStringBuffer.append(str);
    Parameter[] arrayOfParameter = getParameters();
    if ((arrayOfParameter != null) && (arrayOfParameter.length > 0))
    {
      int j = 0;
      int k = arrayOfParameter.length;
      while (j < k)
      {
        if ((j == 0) && (i == 0))
          localStringBuffer.append("?");
        else
          localStringBuffer.append("&");
        Parameter localParameter = arrayOfParameter[j];
        localStringBuffer.append(CodeUtils.cjkEncode(localParameter.getName())).append("=").append(CodeUtils.cjkEncode(localParameter.getValue().toString()));
        ++j;
      }
    }
    return new Access(localStringBuffer.toString(), true);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2 = paramXMLableReader.getTagName();
      if ("WebURLAttributes".equals(str2))
      {
        String str1;
        if ((str1 = paramXMLableReader.getAttr("url")) != null)
          setURL(str1);
      }
      else if ("Parameters".equals(str2))
      {
        setParameters(ReportXMLUtils.readParameters(paramXMLableReader));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("WebURLAttributes");
    if (getURL() != null)
      paramXMLPrintWriter.attr("url", getURL());
    paramXMLPrintWriter.end();
    Parameter[] arrayOfParameter = getParameters();
    ReportXMLUtils.writeParameters(paramXMLPrintWriter, arrayOfParameter);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    WebURLDef localWebURLDef = (WebURLDef)super.clone();
    Parameter[] arrayOfParameter1 = getParameters();
    if (arrayOfParameter1 != null)
    {
      Parameter[] arrayOfParameter2 = new Parameter[arrayOfParameter1.length];
      for (int i = 0; i < arrayOfParameter2.length; ++i)
        arrayOfParameter2[i] = ((Parameter)arrayOfParameter1[i].clone());
      localWebURLDef.setParameters(arrayOfParameter2);
    }
    return localWebURLDef;
  }
}